/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Ventanas;

import Gestores.DtoObjects.BitacoraDTO;
import Gestores.Entidades.EntidadBitacora;
import Gestores.Entidades.EntidadCuentaPago;
import Gestores.Entidades.EntidadDetalleEgreso;
import Gestores.Entidades.EntidadPagoCredencial;
import Gestores.Entidades.EntidadUsuario;
import Negocios.DetalleEgresoNegocio;
import Negocios.GestionCuentaPagoNegocio;
import Negocios.UsuarioNegocio;
import Util.Acciones;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Lion
 */
public class BuscarDetalleEgreso extends javax.swing.JFrame {

    /**
     * Creates new form BuscarDetalleEgreso
     */
    private GestionDetalleEgreso ventanaReferencia;
    private DefaultComboBoxModel modelusuario;
    private DefaultComboBoxModel modelcuenta;
    private DefaultTableModel modelTable;
    private String campos[] = {"Id Detalle", "Cuenta", "Id Cuenta", "Usuario", "Id Usuario", "Monto", "Fecha"};
    //// c.Descripcion, e.Monto, e.Fecha, u.Nombre,e.Id, e.Id_Cuenta, e.Id_Usuario
    private ArrayList<Object> cuenta;
    private ArrayList<Object> usuario;
    private GestionCuentaPagoNegocio negocioCuenta;
    private UsuarioNegocio usuarioNegocio;
    private DetalleEgresoNegocio negocio;
    private EntidadBitacora b;
    private BitacoraDTO dto;

    public void enlazar(GestionDetalleEgreso Referencia) {
        this.ventanaReferencia = Referencia;
    }

    public BuscarDetalleEgreso() {
        initComponents();
        this.modelcuenta = new DefaultComboBoxModel();
        this.modelusuario = new DefaultComboBoxModel();
        this.modelcuenta.addElement("Selecionar Cuenta");
        this.modelusuario.addElement("Seleccionar Usuario");
        this.negocioCuenta = new GestionCuentaPagoNegocio();
        this.usuarioNegocio = new UsuarioNegocio();
        this.modelTable = new DefaultTableModel(campos, 0);
        this.dto = new BitacoraDTO();
        this.table.setModel(modelTable);
        this.negocio = new DetalleEgresoNegocio();
        try {
            this.cuenta = this.negocioCuenta.listarT();
            this.usuario = this.usuarioNegocio.listarUsuarios();
            for (int i = 0; i < this.cuenta.size(); i++) {
                EntidadCuentaPago cp = (EntidadCuentaPago) this.cuenta.get(i);
                this.modelcuenta.addElement(cp.getDescripcion());
            }
            for (int i = 0; i < this.usuario.size(); i++) {
                EntidadUsuario u = (EntidadUsuario) this.usuario.get(i);
                this.modelusuario.addElement(u.getNombre());
            }
            this.cmb_cuenta.setModel(this.modelcuenta);
            this.cmb_usuario.setModel(this.modelusuario);
        } catch (Exception ex) {
            this.txt_mensaje.setText(ex.getMessage());
            Logger.getLogger(BuscarDetalleEgreso.class.getName()).log(Level.SEVERE, null, ex);
        }
        this.table.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {

                int row = table.getSelectedRow();
                int col = table.getSelectedColumn();
                //private String campos[] = {"Cuenta", "Usuario", "Monto", "Fecha"};   
                EntidadDetalleEgreso detalle = new EntidadDetalleEgreso();
                //"Id Detalle","Cuenta","Id Cuenta", "Usuario","Id Usuario", "Monto", "Fecha"};
                detalle.setId_usuario((Integer) table.getValueAt(row, 4));
                detalle.setId_cuenta((Integer) table.getValueAt(row, 2));
                detalle.setMonto((Float) table.getValueAt(row, 5));
                detalle.setFecha((java.sql.Date) table.getValueAt(row, 6));
                detalle.setCuenta((String) table.getValueAt(row, 1));
                detalle.setId((Integer) table.getValueAt(row, 0));

                ventanaReferencia.cargarDatos(detalle);
                BuscarDetalleEgreso.this.dispose();
                BuscarDetalleEgreso.this.dispose();

            }
        });

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        chb_fecha = new javax.swing.JCheckBox();
        chb_cuenta = new javax.swing.JCheckBox();
        chb_monto = new javax.swing.JCheckBox();
        chb_usuario = new javax.swing.JCheckBox();
        jPanel3 = new javax.swing.JPanel();
        Fecha = new javax.swing.JLabel();
        cmb_date = new com.toedter.calendar.JDateChooser();
        jLabel4 = new javax.swing.JLabel();
        cmb_cuenta = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        txt_monto = new javax.swing.JFormattedTextField();
        Usuario = new javax.swing.JLabel();
        cmb_usuario = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        table = new javax.swing.JTable();
        jPanel4 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        txt_mensaje = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Colegio Economista");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Criterio De Busqueda"));

        jPanel2.setLayout(new java.awt.GridLayout(2, 2));

        chb_fecha.setText("Por Fecha");
        jPanel2.add(chb_fecha);

        chb_cuenta.setText("Por Cuenta");
        jPanel2.add(chb_cuenta);

        chb_monto.setText("Por Monto");
        jPanel2.add(chb_monto);

        chb_usuario.setText("Por Usuario");
        jPanel2.add(chb_usuario);

        jPanel3.setLayout(new java.awt.GridLayout(4, 2));

        Fecha.setText("Fecha");
        jPanel3.add(Fecha);
        jPanel3.add(cmb_date);

        jLabel4.setText("Cuenta");
        jPanel3.add(jLabel4);

        cmb_cuenta.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jPanel3.add(cmb_cuenta);

        jLabel5.setText("Monto");
        jPanel3.add(jLabel5);
        jPanel3.add(txt_monto);

        Usuario.setText("Usuario");
        jPanel3.add(Usuario);

        cmb_usuario.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jPanel3.add(cmb_usuario);

        table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }

        )

    );
    table.setColumnSelectionAllowed(false);
    jScrollPane1.setViewportView(table);

    jPanel4.setLayout(new java.awt.GridLayout(1, 0));

    jButton1.setText("Nuevo");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton1ActionPerformed(evt);
        }
    });
    jPanel4.add(jButton1);

    jButton2.setText("Buscar");
    jButton2.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton2ActionPerformed(evt);
        }
    });
    jPanel4.add(jButton2);

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 217, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 311, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap())
        .addComponent(jScrollPane1)
    );
    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(7, 7, 7)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(0, 12, Short.MAX_VALUE))
    );

    jLabel1.setText("Mensaje");

    txt_mensaje.setColumns(20);
    txt_mensaje.setRows(5);
    jScrollPane2.setViewportView(txt_mensaje);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jLabel1)
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap(116, Short.MAX_VALUE)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 476, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap())
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jLabel1)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(33, Short.MAX_VALUE))
    );

    pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        this.chb_cuenta.setSelected(false);
        this.chb_fecha.setSelected(false);
        this.chb_monto.setSelected(false);
        this.chb_usuario.setSelected(false);
        this.txt_monto.setText("");
        this.txt_mensaje.setText("");
        this.cmb_cuenta.setSelectedIndex(0);
        this.cmb_usuario.setSelectedIndex(0);
        this.cmb_date.setDate(null);
        this.modelTable = new DefaultTableModel(campos, 0);
        this.table.setModel(modelTable);

    }//GEN-LAST:event_jButton1ActionPerformed
    private int codUsuario(String nombre) {
        int i = 1;
        while (i <= this.usuario.size()) {
            EntidadUsuario u = (EntidadUsuario) this.usuario.get(i);
            //if(u.getNombre())
        }
        return 0;
    }
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        if (this.chb_cuenta.isSelected() && !this.chb_fecha.isSelected()
                && !this.chb_monto.isSelected() && !this.chb_usuario.isSelected()) {
            if (this.cmb_cuenta.getSelectedIndex() != 0) {
                EntidadCuentaPago c = (EntidadCuentaPago) this.cuenta.get(this.cmb_cuenta.getSelectedIndex() - 1);
                try {
                    ArrayList<Object> e = this.negocio.buscarDetalleEgresoCuenta(c.getId());
                    if (e.size() > 0) {
                        this.modelTable = this.negocio.tabularDatos(e);
                        this.table.setModel(modelTable);
                        this.b = new EntidadBitacora("Criterio cuenta " + (String)this.cmb_cuenta.getSelectedItem(), Principal.Usuario,
                                Principal.CajaChica, Acciones.Select);

                        this.dto.insertar(b);

                    } else {
                        this.txt_mensaje.setText("No se realizo alguna transaccion con esa cuenta");
                    }
                } catch (Exception ex) {
                    this.txt_mensaje.setText(ex.getMessage());
                    Logger.getLogger(BuscarDetalleEgreso.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        if (!this.chb_cuenta.isSelected() && !this.chb_fecha.isSelected()
                && !this.chb_monto.isSelected() && this.chb_usuario.isSelected()) {
            if (this.cmb_usuario.getSelectedIndex() != 0) {
                EntidadUsuario u = (EntidadUsuario) this.usuario.get(this.cmb_usuario.getSelectedIndex() - 1);
                try {
                    ArrayList<Object> e = this.negocio.buscarDetalleEgresoUsuario(u.getId());
                    if (e.size() > 0) {
                        this.modelTable = this.negocio.tabularDatos(e);
                        this.table.setModel(modelTable);
                        this.b = new EntidadBitacora("Criterio Usuario " + (String)this.cmb_usuario.getSelectedItem(), Principal.Usuario,
                                Principal.CajaChica, Acciones.Select);

                        this.dto.insertar(b);
                    } else {
                        this.txt_mensaje.setText("El afiliado no realizo alguna transaccion");
                    }
                } catch (Exception ex) {
                    this.txt_mensaje.setText(ex.getMessage());
                    Logger.getLogger(BuscarDetalleEgreso.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        if (!this.chb_cuenta.isSelected() && this.chb_fecha.isSelected()
                && !this.chb_monto.isSelected() && !this.chb_usuario.isSelected()) {
            if (this.cmb_date.getDate() != null) {
                try {
                    ArrayList<Object> e = this.negocio.buscarDetalleEgresoFecha(this.cmb_date.getDate());
                    if (e.size() > 0) {
                        this.modelTable = this.negocio.tabularDatos(e);
                        this.table.setModel(modelTable);
                        this.b = new EntidadBitacora("Criterio fecha " + this.cmb_date.toString(), Principal.Usuario,
                                Principal.CajaChica, Acciones.Select);

                        this.dto.insertar(b);
                    } else {
                        this.txt_mensaje.setText("No se hiso alguna transaccion en esa fecha");
                    }
                } catch (Exception ex) {
                    this.txt_mensaje.setText(ex.getMessage());
                    Logger.getLogger(BuscarDetalleEgreso.class.getName()).log(Level.SEVERE, null, ex);
                }
            } else {
                this.txt_mensaje.setText("Debe Escribir La Fecha");
            }

        }
        if (!this.chb_cuenta.isSelected() && !this.chb_fecha.isSelected()
                && this.chb_monto.isSelected() && !this.chb_usuario.isSelected()) {
            if (this.txt_monto.getText() != null && this.txt_monto.getText() != "") {
                try {
                    ArrayList<Object> e = this.negocio.buscarDetalleEgresoMonto(Float.valueOf(this.txt_monto.getText()));
                    if (e.size() > 0) {
                        this.modelTable = this.negocio.tabularDatos(e);
                        this.table.setModel(modelTable);
                        this.b = new EntidadBitacora("Criterio monto " + this.txt_monto.getText(), Principal.Usuario,
                                Principal.CajaChica, Acciones.Select);

                        this.dto.insertar(b);
                    } else {
                        this.txt_mensaje.setText("No se hicieron transacciones cuyas transacciones que superen " + this.txt_monto.getText());
                    }
                } catch (Exception ex) {
                    Logger.getLogger(BuscarDetalleEgreso.class.getName()).log(Level.SEVERE, null, ex);
                }
            } else {
                this.txt_mensaje.setText("Debe introducir el monto");
            }
        }
        if (this.chb_cuenta.isSelected() && this.chb_fecha.isSelected()
                && this.chb_monto.isSelected() && this.chb_usuario.isSelected()) {
            if (this.cmb_cuenta.getSelectedIndex() != 0 && this.cmb_usuario.getSelectedIndex() != 0
                    && this.txt_monto.getText() != null && this.cmb_date.getDate() != null) {
                //buscarDetalleEgresoTodosCriterios(int cuenta, int usua, Date d,float m)
                EntidadCuentaPago c = (EntidadCuentaPago) this.cuenta.get(this.cmb_cuenta.getSelectedIndex() - 1);
                EntidadUsuario u = (EntidadUsuario) this.usuario.get(this.cmb_usuario.getSelectedIndex() - 1);

                try {
                    ArrayList<Object> e = this.negocio.buscarDetalleEgresoTodosCriterios(
                            c.getId(), u.getId(), this.cmb_date.getDate(),
                            Float.valueOf(this.txt_monto.getText()));
                    if (e.size() > 0) {
                        this.modelTable = this.negocio.tabularDatos(e);
                        this.table.setModel(modelTable);
                        this.b = new EntidadBitacora("todos los criterios " + (String)this.cmb_cuenta.getSelectedItem()+
                                "-"+this.cmb_date.toString()+"-"+(String)this.cmb_usuario.getSelectedItem()+"-"+
                                this.txt_monto.getText(), Principal.Usuario,
                                Principal.CajaChica, Acciones.Select);

                        this.dto.insertar(b);
                    } else {
                        this.txt_mensaje.setText("No se hicieron transacciones cuyas transacciones que superen " + this.txt_monto.getText());
                    }
                } catch (Exception ex) {
                    Logger.getLogger(BuscarDetalleEgreso.class.getName()).log(Level.SEVERE, null, ex);
                }

            } else {
                this.txt_mensaje.setText("Los campos no deben estar vacios");
            }
        }else{
            this.txt_mensaje.setText("No se selecciono algun criterio de busqueda");
        }

    }//GEN-LAST:event_jButton2ActionPerformed
    private int buscarCodigoCuenta() {
        return 0;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;


                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(BuscarDetalleEgreso.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(BuscarDetalleEgreso.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(BuscarDetalleEgreso.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(BuscarDetalleEgreso.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new BuscarDetalleEgreso().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel Fecha;
    private javax.swing.JLabel Usuario;
    private javax.swing.JCheckBox chb_cuenta;
    private javax.swing.JCheckBox chb_fecha;
    private javax.swing.JCheckBox chb_monto;
    private javax.swing.JCheckBox chb_usuario;
    private javax.swing.JComboBox cmb_cuenta;
    private com.toedter.calendar.JDateChooser cmb_date;
    private javax.swing.JComboBox cmb_usuario;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable table;
    private javax.swing.JTextArea txt_mensaje;
    private javax.swing.JFormattedTextField txt_monto;
    // End of variables declaration//GEN-END:variables
}
